package yl.hs.bmipfull.controllers.host;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import yl.hs.bmipfull.controllers.ControllerBase;
import yl.hs.bmipfull.pojo.production.ProductionLineEntity;
import yl.hs.bmipfull.pojo.production.ProductionTaskEntity;
import yl.hs.bmipfull.security.HostValidation;
import yl.hs.bmipfull.service.laboratory.ITaskConfigDetailsService;
import yl.hs.bmipfull.service.laboratory.ITaskConfigService;
import yl.hs.bmipfull.service.production.IProductShipOrderService;
import yl.hs.bmipfull.service.production.IProductionLinePortService;
import yl.hs.bmipfull.service.production.IProductionTaskService;
import yl.hs.bmipfull.service.production.ITransTaskService;
import yl.hs.bmipfull.viewmodels.WebResult;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author 张凯
 * @apiNote
 * @since 2021/11/17 4:00 下午 创建
 */
@Api(value = "主机生产信息Web接口", tags = "主机程序——主机生产信息操作")
@RequestMapping("/host/production")
@RestController
public class LineProductionController extends ControllerBase {

    @Autowired
    IProductionTaskService taskService;

    @Autowired
    ITransTaskService transTaskService;

    @Autowired
    ITaskConfigService taskConfigService;

    @Autowired
    ITaskConfigDetailsService taskConfigDetailsService;

    @Autowired
    IProductionLinePortService linePortService;


    @Autowired
    IProductShipOrderService shipOrderService;

    @ApiOperation(value = "获取当前生产线的派车信息", notes = "获取当前生产线的派车信息")
    @RequestMapping(value = "/trans/task", method = RequestMethod.GET)
    public WebResult getTransTask() {
        ProductionLineEntity line = HostValidation.getLine();
        assert line != null;
        return success("获取成功", transTaskService.selectByLineSn(line.getSn()));
    }
    @ApiOperation(value = "修改派车信息状态", notes = "修改派车信息状态")
    @RequestMapping(path = "/updateStateTran/{sn}/{state}", method = RequestMethod.POST)
    public WebResult updateStateTrans(@ApiParam(value = "编号") @PathVariable String sn, @ApiParam(value = "状态")@PathVariable short state) {
        if (transTaskService.updateBySn(state,sn) > 0) {
            return success("修改成功");
        } else {
            return fail("修改失败");
        }

    }
    @ApiOperation(value = "获取生产线生产任务信息", notes = "获取生产线生产任务信息")
    @RequestMapping(value = "/task", method = RequestMethod.GET)
    public WebResult getTask() {
        ProductionLineEntity line = HostValidation.getLine();
        List<ProductionTaskEntity> productionTaskEntities = taskService.selectByLineSn(line);
        List<Map<String,Object>> result=new ArrayList<>();
        for (var entity: productionTaskEntities ) {
            Map<String,Object> map=entity.toMap();
            var taskConfig=taskConfigService.queryConfigEntityByTaskSN(entity.getSn());
            if (taskConfig!=null && taskConfig.size()>0){
                map.put("TaskConfigName",taskConfig.get(0).getConfigName());
            }
            result.add(map);
        }
        return success("获取成功", result);
    }

    @ApiOperation(value = "将生产任务改为生产中", notes = "根据生产任务编号将生产任务改为生产中")
    @RequestMapping(value = "/task/production_write/{taskSn}", method = RequestMethod.POST)
    public WebResult updateTaskToProduction(@PathVariable String taskSn){
        Integer integer = taskService.updateTaskWrite(taskSn);
        if (integer != null && integer >0){
            return  success("更改成功");
        } else {
            return error("更改失败");
        }
    }


    @ApiOperation(value = "获取生产任务配比", notes = "获取生产任务配比")
    @RequestMapping(value = "/task/config/{taskSn}", method = RequestMethod.GET)
    public WebResult getTaskConfig(@PathVariable String taskSn) {
        ProductionLineEntity line = HostValidation.getLine();
        return success("获取成功", taskConfigDetailsService.queryByTaskSn(taskSn,line.getSn()));
    }

    @GetMapping("/deliver/info")
    @ApiOperation(value = "获取当前生产线发货情况", notes = "获取当前生产线发货情况")
    public WebResult getDeliver(){
        ProductionLineEntity line = HostValidation.getLine();

        return success("成功",shipOrderService.getDeliverInfoByLineSn(line.getSn()));
    }


    @GetMapping("/port")
    @ApiOperation(value = "获取当前生产线物料口信息", notes = "获取当前生产线物料口信息")
    public WebResult queryLinePort(){
        ProductionLineEntity line = HostValidation.getLine();
        return success("成功",linePortService.queryListPort(line.getSn()));
    }

}
